package sk.fiit.icp.welcome;

import org.apache.wicket.Page;
import org.apache.wicket.Session;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.response.filter.AjaxServerAndClientTimeFilter;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;

import sk.fiit.icp.calendar.view.HomePage;
import sk.fiit.icp.components.SignInSession;
import sk.fiit.icp.components.ajax.DataService;
import sk.fiit.icp.contactmanager.view.ContactManagerPage;
import sk.fiit.icp.event.view.EventPage;
import sk.fiit.icp.eventmanager.view.EventManagerPage;
import sk.fiit.icp.notemanager.view.NoteManagerPage;
import sk.fiit.icp.settings.view.SettingsPage;
import sk.fiit.icp.welcome.view.WelcomePage;

/**
 * A role-authorized, authenticated web application in just a few lines of code.
 * 
 * @author Jonathan Locke
 */
public class WicketApplication extends AuthenticatedWebApplication {

	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	@Override
	public Class<? extends Page> getHomePage() {
		if ( ((SignInSession) Session.get()).isSignedIn() ) {
			return HomePage.class;
		} else {
			return WelcomePage.class;
		}
	}

	/**
	 * @see org.apache.wicket.authentication.AuthenticatedWebApplication#init()
	 */
	@Override
	protected void init() {
		super.init();
		getMarkupSettings().setStripWicketTags(true);

		mountPage("Event", EventPage.class);
		mountPage("EventManager", EventManagerPage.class);
		mountPage("ContactManager", ContactManagerPage.class);
		mountPage("NoteManager", NoteManagerPage.class);
		mountPage("API", DataService.class);
		mountPage("Settings", SettingsPage.class);

		getComponentInstantiationListeners().add( new SpringComponentInjector( this ) );
		getRequestCycleSettings().addResponseFilter( new AjaxServerAndClientTimeFilter() );
	}

	@Override
	protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {
		return SignInSession.class;
	}

	@Override
	protected Class<? extends WebPage> getSignInPageClass() {
		return WelcomePage.class;
	}
}